package com.leige.utils.email;

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.ImageHtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;
import org.apache.commons.mail.resolver.DataSourceUrlResolver;

public class EmailSendUtils {
	private static final Log logger = LogFactory.getLog(EmailSendUtils.class);
	private EmailSetting emailSetting;

	public void setEmailSetting(EmailSetting emailSetting) {
		this.emailSetting = emailSetting;
	}
	 
	private   void setting(org.apache.commons.mail.Email email) throws EmailException {
		email.setHostName(emailSetting.getSmtpHostName());
		email.setSmtpPort(emailSetting.getSmtpPort());
		email.setSSLOnConnect(emailSetting.isSsl());
		email.setAuthenticator(new DefaultAuthenticator(emailSetting.getAuthUserName(), emailSetting.getAuthPassword()));
		email.setFrom(emailSetting.getAuthUserName(),emailSetting.getNoteName(),emailSetting.getCharset());
	}
	private  String send(Email email, EmailAddress[] recipients,EmailAddress[] copytos) throws EmailException {
		if(recipients!=null)
			for (EmailAddress emailAddress : recipients) {
				email.addTo(emailAddress.toString(), emailAddress.getNoteName(), emailSetting.getCharset());
			}
		if(copytos!=null)
			for (EmailAddress emailAddress : copytos) {
				email.addCc(emailAddress.toString(), emailAddress.getNoteName(), emailSetting.getCharset());
			}
		return email.send();
	}
	private Email sendText(String subject,String msg) throws EmailException  {
		Email email = new SimpleEmail();
		setting(email);
		email.setSubject(subject);
		email.setMsg(msg);
		return email;
	}
	public String sendText(String subject,String msg, EmailAddress[] recipients, EmailAddress[] copytos) {
		try {
			return this.send(this.sendText(subject, msg), recipients, copytos);
		} catch (EmailException e) {
			// TODO: handle exception
			logger.error("",e);
			return null;
		}
	}
	public String sendText(String subject,String msg, EmailAddress ... recipients) {
		return this.sendText(subject, msg, recipients, null);
	}
	
	private Email sendAttachment (String subject,String msg,EmailAttachment [] attachments) throws EmailException  {
		MultiPartEmail email = new MultiPartEmail();
		setting(email);
		email.setSubject(subject);
		email.setMsg(msg);
		for (EmailAttachment emailAttachment : attachments) {
			email.attach(emailAttachment);
		}
		return email;
	}
	public String sendAttachment(String subject,String msg,EmailAttachment[] attachments, EmailAddress[] recipients, EmailAddress[] copytos) {
		try {
			return this.send(this.sendAttachment(subject, msg,attachments), recipients, copytos);
		} catch (EmailException e) {
			logger.error("",e);
			return null;
		}
	}
	public String sendAttachment(String subject,String msg,EmailAttachment[] attachments, EmailAddress ... recipients) {
		return this.sendAttachment( subject, msg,attachments , recipients, null);
	}
	public String sendAttachment(String subject,String msg, EmailAddress recipients,EmailAttachment... attachments) {
		return this.sendAttachment( subject, msg,attachments, new EmailAddress[]{recipients}, null);
	}
 
	private Email sendHtml(String subject,String htmlMsg,String textMsg, URL [] urls) throws EmailException  {
		  HtmlEmail email = new HtmlEmail();
		  setting(email);
		  email.setSubject(subject);
		  for (URL url : urls) {
			  String cid = email.embed(url, url.getFile());
			  htmlMsg.replaceAll( url.hashCode()+"", "cid:"+cid);
		  }
		  email.setHtmlMsg(htmlMsg);
		  email.setTextMsg(textMsg);
		  return email;
	}

	public String sendHtml(String subject,String htmlMsg,String textMsg, URL [] urls, EmailAddress[] recipients, EmailAddress[] copytos) {
		try {
			return this.send(this.sendHtml(subject, htmlMsg,textMsg,urls), recipients, copytos);
		} catch (EmailException e) {
			logger.error("",e);
			return null;
		}
	}
	public String sendHtml(String subject,String htmlMsg,String textMsg, URL [] urls, EmailAddress ... recipients) {
		return this.sendHtml( subject, htmlMsg,textMsg,urls , recipients, null);
	}
	public String sendHtml(String subject,String htmlMsg,String textMsg,  EmailAddress recipients,URL... urls) {
		return this.sendHtml( subject, htmlMsg,textMsg,urls, new EmailAddress[]{recipients}, null);
	}
	
	private Email sendImageHtml(String subject,String htmlMsg,String textMsg,String resolverUrl) throws EmailException  {
		  ImageHtmlEmail email = new ImageHtmlEmail();
		  if(resolverUrl!=null)
			  try {
				  URL url= new URL(resolverUrl);
				  email.setDataSourceResolver(new DataSourceUrlResolver(url));
			} catch (MalformedURLException e) {
				logger.warn("初始化地址出错"+resolverUrl,e);
			}
		  setting(email);
		  email.setSubject(subject);
		  email.setHtmlMsg(htmlMsg);
		  email.setTextMsg(textMsg);
		  return email;
	}

	public String sendImageHtml(String subject,String htmlMsg,String textMsg,String resolverUrl, EmailAddress[] recipients, EmailAddress[] copytos) {
		try {
			return this.send(this.sendImageHtml(subject, htmlMsg,textMsg,resolverUrl), recipients, copytos);
		}  catch (EmailException e) {
			logger.error("",e);
			return null;
		}
	}
	public String sendImageHtml(String subject,String htmlMsg,String textMsg,String resolverUrl, EmailAddress ... recipients) {
		return this.sendImageHtml( subject, htmlMsg,textMsg,resolverUrl , recipients, null);
	}
	public String sendImageHtml(String subject,String htmlMsg,String textMsg,  EmailAddress recipients ) {
		return this.sendImageHtml( subject, htmlMsg,textMsg,null, new EmailAddress[]{recipients}, null);
	}
}
